package cn.tedu.vod.service.impl;

import cn.tedu.commonUtils.R;
import cn.tedu.serviceBase.exception.EducationException;
import cn.tedu.vod.service.VodService;
import cn.tedu.vod.utils.AliyunVodSDKUtils;
import cn.tedu.vod.utils.VodConstant;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class VodServiceImpl implements VodService {
    @Override
    public String uploadVideo(MultipartFile file) {

        /**
         * 流式上传接口
         *
         * @param accessKeyId
         * @param accessKeySecret
         * @param title
         * @param fileName
         * @param inputStream
         */
        String accessKeyId = VodConstant.KEY_ID;
        String accessKeySecret = VodConstant.KEY_SECRET;

        String filename = file.getOriginalFilename();

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String prefix = filename.substring(0, filename.lastIndexOf("."));
        String title = uuid+ prefix;

        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, filename, inputStream);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        log.info("RequestId={}", response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        String videoId = response.getVideoId();
        if (response.isSuccess()) {
            log.info("VideoId={}", videoId + "\n");
        } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            log.info("VideoId={}", videoId + "\n");
            log.error("ErrorCode={}", response.getCode() + "\n");
            log.error("ErrorMessage={}", response.getMessage() + "\n");
        }
        return videoId;
    }

    @Override
    public void deleteVideoBatch( List<String> videoIdList) {
        if(videoIdList == null || videoIdList.isEmpty()){
            throw new EducationException(20001,"视频删除失败videoId==null");
        }
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(VodConstant.KEY_ID, VodConstant.KEY_SECRET);
        DeleteVideoRequest request = new DeleteVideoRequest();
        String videoIds = StringUtils.join(videoIdList.toArray(), ",");
        request.setVideoIds(videoIds);
        try {
            DeleteVideoResponse response = client.getAcsResponse(request);
            log.info("RequestId = " + response.getRequestId() + "\n");
        } catch (ClientException e) {
            log.error("视频删除失败:{}",e.getErrMsg());
           e.printStackTrace();
        }
    }
}
